package com.example.demo.controller;


import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import com.example.demo.utility.CustomUtil;
import com.example.demo.utility.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

/**
 * 用户控制器
 *
 * @author yzh
 * @date 2022/08/31
 */
@Controller
public class UserController {

    @Autowired
    @Resource
    private UserMapper userMapper;

    @RequestMapping("user")
    public String user(Model model) {
        User tom = userMapper.selectByName("Tom");
        System.out.println(tom);
        model.addAttribute("list", tom);
        model.addAttribute("msg", "吾等为何而战");
        return "1";
    }

    /**
     * 用户列表
     *
     * @return {@link List}<{@link User}>
     */
    @RequestMapping("/getuser")
    @ResponseBody
    public List<User> user_list() {

        return userMapper.selectList(null);
    }

    /**
     * 第一种添加方法
     *
     * @param user 用户
     * @return {@link String}
     */
    @RequestMapping("/adduser")
    @ResponseBody
    public String adduser(@RequestBody User user) {
        //当前端的axios使用的是params时可以使用HttpServletRequest的request.getParameter("uname")来获取数据,也可以直接String xx获取，不可以使用实体类接收
        //前端使用this.axios.post传值时可以使用@RequestBody User user实体类接收，


        System.out.println("进来了");
        /*System.out.println(request.getParameter("uname"));*/
        System.out.println(user);
        userMapper.insert(user);
        return "1";
    }

    /**
     * 第二种添加
     *
     * @param request 请求
     * @return {@link String}
     */
    @RequestMapping("/adduser2")
    @ResponseBody
    public String adduser2(HttpServletRequest request) {
        System.out.println("进来了");

        System.out.println(request.getParameter("uname"));

        return "1";
    }

    /**
     * 批量删除
     *
     * @param ids id
     * @return {@link Object}
     */
    @RequestMapping("/batch_Delete")
    @ResponseBody
    public Object batch_Delete(String ids) {
        HashMap<Object, Object> map = new HashMap<>();

        String[] idAll = ids.split(",");
        ArrayList<String> list = new ArrayList<>();

        for (String id : idAll) {
            int i = userMapper.deleteById(id);
            if (i == 0) {
                list.add(id);
            }
        }

        if (list.size() == 0) {
            return "1";
        } else {
            return "2";
        }
    }
}
